package org.csu.demopetstore.Controller;

import org.csu.demopetstore.Service.AccountService;
import org.csu.demopetstore.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    //跳转到登录界面
    @RequestMapping("/SignonForm")
    public String signonForm(){
        return "account/SignonForm";
    }

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session,Model model){
        System.out.println(username);
        System.out.println(password);
        if( !("".equals(username)) && !("".equals(password))){
            Account account = accountService.login(username,password);
            if (account == null){
                String errorValue = "账户或密码有误！";
                model.addAttribute("errorValue",errorValue);
                return "account/SignonForm";
            }else {
                model.addAttribute(account);
                session.setAttribute("account", account);
                return "/catalog/main";
            }
        }else {
            String errorValue = "输出不能为空！";
            model.addAttribute("errorValue",errorValue);
            return "account/SignonForm";
        }
    }

    @RequestMapping("/signout")
    public String signoutForm(HttpSession session){
        session.removeAttribute("account");
        return "catalog/main";
    }

    @RequestMapping("/myAccount")
    public String myAccount(Model model, HttpSession session){
        Account accountLogin = (Account)session.getAttribute("account");
        String username = accountLogin.getUsername();
        String password = accountLogin.getPassword();
        Account account = accountService.getAccountInformation(username);
        account.setPassword(password);
        model.addAttribute(account);
        session.setAttribute("account",account);
        return "account/EditAccountForm";
    }

    @RequestMapping("/updateAccount")
    public String updateAccount(@RequestParam("username")String username,
                                @RequestParam("repeatedPassword")String repeatedPassword,
                                @RequestParam("languagePreference")String languagePreference,
                                @RequestParam("favouriteCategoryId")String favouriteCategoryId,
                                @RequestParam(value = "listOption",required = false)String listOption,
                                @RequestParam(value = "bannerOption",required = false)String bannerOption,
                                @RequestParam("firstname")String firstname,
                                @RequestParam("lastname")String lastname,
                                @RequestParam("email")String email,
                                @RequestParam("phone")String phone,
                                @RequestParam("address1")String address1,
                                @RequestParam("address2")String address2,
                                @RequestParam("city")String city,
                                @RequestParam("state")String state,
                                @RequestParam("zip")String zip,
                                @RequestParam("country")String country,
                                HttpSession session){
        Account account = (Account)session.getAttribute("account");
        System.out.println(account);
        account.setUsername(username);
        account.setPassword(repeatedPassword);
        account.setPhone(phone);
        account.setCountry(country);
        account.setState(state);
        account.setCity(city);
        account.setAddress1(address1);
        account.setAddress2(address2);
        //account.setStatus(status);
        account.setFirstName(firstname);
        account.setLastName(lastname);
        account.setLanguagePreference(languagePreference);
        account.setZip(zip);
        account.setFavouriteCategoryId(favouriteCategoryId);
        account.setEmail(email);
        account.setListOption(Boolean.parseBoolean(listOption));
        account.setBannerOption(Boolean.parseBoolean(bannerOption));
        System.out.println(account);
        accountService.updateAccount(account);
        accountService.updateProfile(account);
        accountService.updateSignon(account);
        return "account/SignonForm";
    }

    @RequestMapping("/register")
    public String register(Model model, HttpSession session){
        return "account/Register";
    }

    @RequestMapping("/insertAccount")
    public String insertAccount(@RequestParam("username")String username,
                                @RequestParam("repeatedPassword")String repeatedPassword,
                                @RequestParam("languagePreference")String languagePreference,
                                @RequestParam("favouriteCategoryId")String favouriteCategoryId,
                                @RequestParam(value = "listOption",required = false)String listOption,
                                @RequestParam(value = "bannerOption",required = false)String bannerOption,
                                @RequestParam("firstname")String firstname,
                                @RequestParam("lastname")String lastname,
                                @RequestParam("email")String email,
                                @RequestParam("phone")String phone,
                                @RequestParam("address1")String address1,
                                @RequestParam("address2")String address2,
                                @RequestParam("city")String city,
                                @RequestParam("state")String state,
                                @RequestParam("zip")String zip,
                                @RequestParam("country")String country){
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(repeatedPassword);
        account.setPhone(phone);
        account.setCountry(country);
        account.setState(state);
        account.setCity(city);
        account.setAddress1(address1);
        account.setAddress2(address2);
        //account.setStatus(status);
        account.setFirstName(firstname);
        account.setLastName(lastname);
        account.setLanguagePreference(languagePreference);
        account.setZip(zip);
        account.setFavouriteCategoryId(favouriteCategoryId);
        account.setEmail(email);
        account.setListOption(Boolean.parseBoolean(listOption));
        account.setBannerOption(Boolean.parseBoolean(bannerOption));
        System.out.println(account);
        accountService.insertAccount(account);
        accountService.insertProfile(account);
        accountService.insertSignon(account);
        return "account/SignonForm";
    }
}
